package com.zbf.stream2;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author: LI-CI-GAO
 * 作者: licigao
 * 日期: 2021/8/18  11:48
 * 描述:
 */
public class Test1811Stream2 {

    public static void main(String[] args) {

        List<Map<String,Object>> list=new ArrayList<>();
        Map<String,Object> map1=new HashMap<>();
        map1.put("name","张三");
        map1.put("age",12);
        Map<String,Object> map11=new HashMap<>();
        map11.put("name","张三");
        map11.put("age",15);
        Map<String,Object> map111=new HashMap<>();
        map111.put("name","张三");
        map111.put("age",15);
        Map<String,Object> map2=new HashMap<>();
        map2.put("name","张三2");
        map2.put("age",12);
        Map<String,Object> map3=new HashMap<>();
        map3.put("name","李四");
        map3.put("age",10);
        Map<String,Object> map33=new HashMap<>();
        map33.put("name","李四");
        map33.put("age",10);
        Map<String,Object> map4=new HashMap<>();
        map4.put("name","王五");
        map4.put("age",11);

        list.add(map1);
        list.add(map2);
        list.add(map3);
        list.add(map4);
        list.add(map33);
        list.add(map11);
        list.add(map111);


        Map<String, List<Map<String, Object>>> name = list.stream().collect(Collectors.groupingBy(map -> {
            return map.get("name").toString();
        }));
        System.out.println(name);


        //去除重复
        //简单的去重复
        List<Map<String, Object>> collect = list.stream().distinct().collect(Collectors.toList());
        System.out.println("-->"+collect);
        //自定义去除重复
        ArrayList<Map<String, Object>> collect1 =
                list.stream().collect(
                        Collectors.collectingAndThen(
                                Collectors.toCollection(
                                        () -> new TreeSet<>(
                                                Comparator.comparing(m -> m.get("name").toString()+""+m.get("age"))
                                        )
                                ),
                                ArrayList::new)
                );


        System.out.println(collect1);


        //聚合操作(返回所有的最大值)
        Map.Entry<String, List<Map<String, Object>>> age =
                list.stream().collect(
                        Collectors.groupingBy(
                                ma -> ma.get("age").toString(), TreeMap::new, Collectors.toList()
                        )
                ).lastEntry();

        Map.Entry<String, List<Map<String, Object>>> age1 = list.stream().collect(
                Collectors.groupingBy(
                        ma -> ma.get("age").toString(), TreeMap::new, Collectors.toList()
                )
        ).firstEntry();

        //System.out.println(age);
        System.out.println(age1);



        //map转list
        Map<String,Object> map55=new HashMap<>();
        map55.put("name","张三");
        map55.put("age",12);
        List<String> collect2 = map55.entrySet().stream().map(entry -> {
            return entry.getKey() + "=" + entry.getValue();
        }).collect(Collectors.toList());
        System.out.println(collect2);

        //list转map
       /* Map<String, Map<String, Object>> age2 = list.stream().collect(
                Collectors.toMap(
                        m1 -> {
                            return m1.get("age").toString();
                        },
                        m1 -> {
                            return m1;
                        }
                )
        );*/

        Map<String, Map<String, Object>> age3 = list.stream().collect(
                Collectors.toMap(
                        m1 -> {
                            return m1.get("age").toString();
                        },
                        m1 -> {
                            return m1;
                        },
                        (k1, k2) -> {
                            return k2;
                        }
                )
        );


        System.out.println(age3);

    }

}
